<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="动物名称" prop="animalBy2">
        <el-input
          v-model="queryParams.animalBy2"
          placeholder="请输入动物名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="animalNumber">
        <el-input
          v-model="queryParams.animalNumber"
          placeholder="请输入数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['animalManager:animalManager:add']"
        >新动物入库</el-button>
      </el-col>
      <el-col :span="1.5">
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['animalManager:animalManager:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="animalManagerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="动物编号" align="center" prop="animalId" />
      <el-table-column label="动物序种属编号" align="center" prop="animalName" />
      <el-table-column label="动物名称" align="center" prop="animalBy2" />
      <el-table-column label="数量" align="center" prop="animalNumber" />
      <el-table-column label="更新时间" align="center" prop="animalDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.animalDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['animalManager:animalManager:edit']"
          >出库</el-button>


          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handledit(scope.row)"
            v-hasPermi="['animalManager:animalManager:edit']"
          >入库</el-button>
        </template>


       
      </el-table-column>
    </el-table>
    



    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加动物库存管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="动物名称" prop="animalBy2">
          <el-input v-model="form.animalBy2" placeholder="请输入动物名称" />
        </el-form-item>
        <el-form-item label="动物种属编号" prop="animalName">
          <el-input v-model="form.animalName" placeholder="请输入动物种属编号" />
        </el-form-item>
        <el-form-item label="动物种属名称" prop="animalMsg">
          <el-input v-model="form.animalMsg" placeholder="请输入动物种属名称" />
        </el-form-item>
        <el-form-item label="更新时间" prop="animalDate">
          <el-date-picker clearable
            v-model="form.animalDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>

          <el-form-item label="笼子大小" prop="cageId">
                <el-select v-model="form.cageSizeId" placeholder="请选择笼子大小">
                  <el-option 
                  v-for="(cage, index) in cageList"
                    :key="index"
                    :label="cage.cageSize"
                    :value="cage.cageSizeId"
                  ></el-option>
                </el-select>
              </el-form-item>      

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--出库模态框-->

    <el-dialog :title="title" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="动物名称" prop="animalName">
          <el-input v-model="form.animalBy2" placeholder="请输入动物名称" />
        </el-form-item>
        <el-text>出库数量:1</el-text>
        <el-form-item label="更新时间" prop="animalDate">
          <el-date-picker clearable
            v-model="form.animalDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>

    <!--入库模态框-->

    <el-dialog :title="title" :visible.sync="open2" width="500px" append-to-body>
     
  
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="动物名称" prop="animalName">
          <el-input v-model="form.animalBy2" placeholder="请输入动物名称" />
        </el-form-item>
        <el-form-item label="数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="更新时间" prop="animalDate">
          <el-date-picker clearable
            v-model="form.animalDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {goDown,queryCageSize, listAnimalManager, getAnimalManager, addAnimalManager, updateAnimalManager } from "@/api/animalManager/animalManager";

export default {
  name: "AnimalManager",
  data() {
    return {
      //所有笼子类型的
      cageList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 动物库存管理表格数据
      animalManagerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      open2:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        animalName: null,
        animalNumber: null,
        animalSex: null,
        animalDate: null,
        cageSizeId: null,
        animalBy1: null,
        animalBy2: null,
        animalBy3: null,
        dateRange:[]
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.loadStaffFeedingRecord();
  },
  methods: {
       /** 入库按钮的提交操作 */
    submitForm1(){
      this.$refs["form"].validate(valid => {
        console.log(this.form)
        if (valid) {
            goDown(this.form).then(response => {
              if(response.code==200){
              this.$modal.msgSuccess("入库成功");
              this.open2 = false;
              this.getList();
              }else{
                this.$message("笼子不足");
              }

            });
           
        }
      });

    },
       /** 查询所有的笼子尺寸信息 */
      loadStaffFeedingRecord(){
        this.loading = true;
        queryCageSize().then(response=>{
            this.cageList = response.data;
          
            this.loading = false;
        });
      },
    /** 查询动物库存管理列表 */
    getList() {
      this.loading = true;
      listAnimalManager(this.queryParams).then(response => {
        this.animalManagerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancel1() {
      this.open1 = false;
      this.reset();
    },
    cancel2() {
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        animalId: null,
        animalName: null,
        animalNumber: null,
        animalSex: null,
        animalDate: null,
        cageSizeId: null,
        animalBy1: null,
        animalBy2: null,
        animalBy3: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.animalId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增动物管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const animalId = row.animalId || this.ids
      getAnimalManager(animalId).then(response => {
        this.form = response.data;
        this.open1 = true;
        this.title = "动物出库管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.animalId != null) {
            updateAnimalManager(this.form).then(response => {
              if(response.code==200){
              this.$modal.msgSuccess("出库成功");
              this.open1 = false;
              this.getList();
              }else{
                this.$message("库存不足");
              }
            
            });
          } else {
            console.log(this.form)
            addAnimalManager(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
             
            });
          }
        }
      });
    },
    /** 入库按钮操作 */
       
    handledit(row) {
      this.reset();
      const animalId = row.animalId || this.ids
      getAnimalManager(animalId).then(response => {
        this.form = response.data;
        this.open2 = true;
        this.title = "动物入库管理";
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('animalManager/animalManager/export', {
        ...this.queryParams
      }, `animalManager_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
